package com.example.demo.nuoqi.controller;

import com.example.demo.nuoqi.dao.MyapplicationMapper;
import com.example.demo.nuoqi.pojo.*;
import com.example.demo.nuoqi.service.ApplyapprovalService;
import com.example.demo.nuoqi.service.ItemdetailsService;
import com.example.demo.nuoqi.service.MyapplicationService;
import com.example.demo.nuoqi.service.UserService;
import com.example.demo.nuoqi.utils.*;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "ApplyapprovalController")
@RestController
@RequestMapping("/applyapproval")
@CrossOrigin
public class ApplyapprovalController {

    @Autowired
    private ApplyapprovalService applyapprovalService;
    @Autowired
    MyapplicationService myapplicationService;
    @Autowired
    MyapplicationMapper myapplicationMapper;
    @Autowired
    UserService userService;
    @Autowired
    ItemdetailsService itemdetailsService;

    /***
     * Applyapproval分页条件搜索实现
     * @param applyapproval
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Applyapproval条件分页查询",notes = "分页条件查询Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Applyapproval对象",value = "传入JSON数据",required = false) Applyapproval applyapproval, @PathVariable int page, @PathVariable int size){
        //调用ApplyapprovalService实现分页条件查询Applyapproval
        PageInfo<Applyapproval> pageInfo = applyapprovalService.findPage(applyapproval, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Applyapproval分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Applyapproval分页查询",notes = "分页查询Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用ApplyapprovalService实现分页查询Applyapproval
        PageInfo<Applyapproval> pageInfo = applyapprovalService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param applyapproval
     * @return
     */
    @ApiOperation(value = "Applyapproval条件查询",notes = "条件查询Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @PostMapping(value = "/search" )
    public Result<List<Applyapproval>> findList(@RequestBody(required = false) @ApiParam(name = "Applyapproval对象",value = "传入JSON数据",required = false) Applyapproval applyapproval){
        //调用ApplyapprovalService实现条件查询Applyapproval
        List<Applyapproval> list = applyapprovalService.findList(applyapproval);
        return new Result<List<Applyapproval>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Applyapproval根据ID删除",notes = "根据ID删除Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用ApplyapprovalService实现根据主键删除
        applyapprovalService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Applyapproval数据
     * @param applyapproval
     * @param id
     * @return
     */
    @ApiOperation(value = "Applyapproval根据ID修改",notes = "根据ID修改Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Applyapproval对象",value = "传入JSON数据",required = false) Applyapproval applyapproval, @PathVariable Integer id){
        //设置主键值
        applyapproval.setId(id);
        //调用ApplyapprovalService实现修改Applyapproval
        applyapprovalService.update(applyapproval);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Applyapproval数据
     * @param applyapproval
     * @return
     */
    @ApiOperation(value = "Applyapproval添加",notes = "添加Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Applyapproval对象",value = "传入JSON数据",required = true) Applyapproval applyapproval){
        //调用ApplyapprovalService实现添加Applyapproval
        applyapprovalService.add(applyapproval);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Applyapproval数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Applyapproval根据ID查询",notes = "根据ID查询Applyapproval方法详情",tags = {"ApplyapprovalController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Applyapproval> findById(@PathVariable Integer id){
        //调用ApplyapprovalService实现根据主键查询Applyapproval
        Applyapproval applyapproval = applyapprovalService.findById(id);
        return new Result<Applyapproval>(true,StatusCode.OK,"查询成功",applyapproval);
    }

    /***
     * 查询Applyapproval全部数据
     * @return
     */
    @ApiOperation(value = "查询当前用户Applyapproval列表方法",notes = "查询当前用户Applyapproval列表方法",tags = {"ApplyapprovalController"})
    @GetMapping("/findAllByUser")
    public Result<List<Applyapproval>> findAllByUser(HttpSession session){
        User user = UserUtils.getUser(session);
        //调用ApplyapprovalService实现查询所有Applyapproval
        List<Applyapproval> list = applyapprovalService.findAll();
        ArrayList<Applyapproval> temp = new ArrayList<>();
        for (Applyapproval applyapproval : list) {
            Integer ownUserId = applyapproval.getOwnUserId();
            if (user.getId() == ownUserId) {
                temp.add(applyapproval);
            }
        }

        return new Result<List<Applyapproval>>(true, StatusCode.OK,"查询成功",temp) ;
    }

    /***
     * 查询Applyapproval全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Applyapproval",notes = "查询所Applyapproval有方法详情",tags = {"ApplyapprovalController"})
    @GetMapping
    public Result<List<Applyapproval>> findAll(){
        //调用ApplyapprovalService实现查询所有Applyapproval
        List<Applyapproval> list = applyapprovalService.findAll();
        return new Result<List<Applyapproval>>(true, StatusCode.OK,"查询成功",list) ;
    }


    /***
     * 设置审批结果
     * @return
     */
    @ApiOperation(value = "设置审批结果----1同意, 0 拒绝",notes = "设置审批结果方法详情",tags = {"ApplyapprovalController"})
    @GetMapping("setApprovalResult/{chooseId}/{result}")
    public Result setApprovalResult(@PathVariable("chooseId") Integer id,@PathVariable("result")String result){
        //调用ApplyapprovalService实现查询所有Applyapproval
        Applyapproval byId = applyapprovalService.findById(id);
        Myapplication myapplication = myapplicationMapper.selectByPrimaryKey(byId.getApplicationId());
        User user = userService.findById(Integer.valueOf(byId.getApprovalPersonId()));
        Itemdetails itemdetails = itemdetailsService.findById(Integer.valueOf(byId.getItemId()));
        if ("1".equals(result)) {
            byId.setStatus(result); //设置审批表中记录同意
            myapplication.setStatus(result); //设置申请人表中状态结果
        }
        if ("2".equals(result)){
            //把申请人 放到项目表中参与人字段里面
            // 临时参与人对象


            Itemdetails.participantTemp temp = new Itemdetails.participantTemp();
            temp.setUserId(user.getId());
            temp.setUserName(user.getName());
            //如果之前存有 就添加
            if (itemdetails.getParticipant() != null || !"".equals(itemdetails.getParticipant())) {
                List<Itemdetails.participantTemp> temps = JsonUtils.jsonToList(itemdetails.getParticipant(), Itemdetails.participantTemp.class);
                if (itemdetails.getItemPersonNum() >= temps.size()-1) {
                    temps.add(temp);
                    //转json 放入item中
                    itemdetails.setParticipant(JsonUtils.objectToJson(temps));
                }else{
                    itemdetails.setItemStatus("2");
                    itemdetailsService.update(itemdetails);
                    return new Result(false, StatusCode.ERROR,"参与人数已满",null) ;
                }
            }else{
                itemdetails.setParticipant(JsonUtils.objectToJson(temp));
            }
            byId.setStatus(result);
            myapplication.setStatus(result);
        }
        // 同意退出
        if ("6".equals(result)) {

            // b false  人没满
            boolean b = ItemUtils.ItemPersonNumLtGt(itemdetails);
            if (b == false) {
                // 项目排除
                Result r1 = ItemUtils.delItemPartPerson(itemdetails, user);

                Itemdetails data = (Itemdetails) r1.getData();

                itemdetails = data;
            }


            byId.setStatus(result);
            myapplication.setStatus(result);
            applyapprovalService.update(byId);
            myapplicationService.update(myapplication);
            itemdetailsService.update(itemdetails);
            return new Result<List<Applyapproval>>(true, StatusCode.OK,"同意退出",null) ;
        }else{
            byId.setStatus(result);
            myapplication.setStatus(result);
        }
        applyapprovalService.update(byId);
        myapplicationService.update(myapplication);
        itemdetailsService.update(itemdetails);
        return new Result<List<Applyapproval>>(true, StatusCode.OK,"审批完成",null) ;
    }

}
